import { reqGetUserAssets, reqGetUserAddress, reqGetUserPoint, reqGetUserCredit, reqGetCountryList } from '@/api/home/account.js'
import { reqGetFailedOrder, reqGetSucceedOrder } from '@/api/home/order.js'

export default {
  state: {
    userAssets: {},
    userAddress: {},
    failedOrder: [],
    succeedOrder: [],
    address: {},
    submitOrderData: {},
    userPoint: [],
    userCredit: [],
    countryList: []
  },
  mutations: {
    initUserAssets(state, data) {
      state.userAssets = data.assetsData
    },
    initUserAddress(state, data) {
      state.userAddress = data.address.item
      var item = state.userAddress.find(o => o.isDefault)
      state.address = item
    },
    // 选择地址
    selectAddress(state, data) {
      state.address = data
    },
    // 提交订单
    submitOrder(state, data) {
      state.submitOrderData = data
    },
    initFailedOrder(state, data) {
      state.failedOrder = data
    },
    initSucceedOrder(state, data) {
      state.succeedOrder = data
    },
    initUserPoint(state, data) {
      state.userPoint = data
    },
    initUserCredit(state, data) {
      state.userCredit = data
    },
    initCountryList(state, data) {
      state.countryList = data
    }
  },
  actions: {
    async getUserAssets({ commit }, data) {
      const result = await reqGetUserAssets(data)
      if (result.code === 200) {
        // console.log(result)
        commit('initUserAssets', result.data)
      } else {
        return result.message
      }
    },
    async getUserAddress({ commit }, data) {
      const result = await reqGetUserAddress(data)
      if (result.code === 200) {
        // console.log(result)
        commit('initUserAddress', result.data)
      } else {
        return result.message
      }
    },
    async getFailedOrder({ commit }, data) {
      const result = await reqGetFailedOrder(data)
      if (result.code === 200) {
        // console.log(result)
        commit('initFailedOrder', result.data.data)
      } else {
        return result.message
      }
    },
    async getSucceedOrder({ commit }, data) {
      const result = await reqGetSucceedOrder(data)
      if (result.code === 200) {
        // console.log(result)
        commit('initSucceedOrder', result.data.data)
      } else {
        return result.message
      }
    },
    async getUserPoint({ commit }, data) {
      const result = await reqGetUserPoint(data)
      if (result.code === 200) {
        // console.log(result)
        commit('initUserPoint', result.data.data)
      } else {
        return result.message
      }
    },
    async getUserCredit({ commit }, data) {
      const result = await reqGetUserCredit(data)
      if (result.code === 200) {
        // console.log(result)
        commit('initUserCredit', result.data.data)
      } else {
        return result.message
      }
    },
    async getCountryList({ commit }, data) {
      const result = await reqGetCountryList(data)
      if (result.code === 200) {
        // console.log(result)
        commit('initCountryList', result.data.country[1].options)
      } else {
        return result.message
      }
    }
  },
  getters: {}
}
